package com.qzsoft.zhjd.common.excel;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;

/**
 * @ClassName ZhjdExcelExportUtils
 * @Description excel导出工具类
 * @author linhuawei
 * @since 2022-04-02
 */
public class ZhjdExcelExportUtils {

    /**
     * @Title exportExcel
     * @Description 导出excel
     * @param response
     * @param fileName
     * @param exportParams
     * @param clasz
     * @param list
     */
    public static void exportExcel(HttpServletResponse response, String fileName, ExportParams exportParams,
            Class<?> clasz, List<?> list) throws IOException {
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, clasz, list);
        exportExcel(response, fileName, workbook);
    }

    /**
     * @Title exportExcel
     * @Description 导出多个sheet页
     * @param response
     * @param fileName
     * @param sheets
     */
    public static void exportExcel(HttpServletResponse response, String fileName, List<Map<String, Object>> sheets)
            throws IOException {
        Workbook workbook = ExcelExportUtil.exportExcel(sheets, ExcelType.HSSF);
        exportExcel(response, fileName, workbook);
    }

    public static void exportExcel(HttpServletResponse response, String fileName, Workbook workbook)
            throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition",
                "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xls");
        ServletOutputStream out = response.getOutputStream();
        workbook.write(out);
        out.flush();
    }

}
